#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QSerialPort>
#include <QObject>
#include <QTimer>

class SerialPort : public QSerialPort
{
    Q_OBJECT
public:
    explicit SerialPort(QObject *parent = nullptr);

public:
    QStringList Port_name;
    QString PortName;
    QByteArray Read_buffer; // 数据缓冲区
    int Timeout_times = 40;

    typedef enum port_event
    {
        EVENT_PORT_CLOSE = 0,
        EVENT_PORT_OPEN = 1,
        EVENT_PORT_DISCONNECT = 2,
        EVENT_PORT_ADD_NEW = 3, /*新设备插入*/
        EVENT_PORT_REDUCE = 4,
        EVENT_PORT_REFRESH = 5
    } E_EVENTS_T;

    void Set_timeout(int ms);
    int Open(QString PortName);
    void Close(void);
    void Write(QByteArray data, qint16 len);

private:
    QTimer Read_timeout;  // 读取超时定时器
    QTimer Write_timeout; // 写入超时定时器

signals:
    void ReadComplete(QByteArray data, qint16 len);
    void WriteComplete(void);
    void Serial_event_signal(E_EVENTS_T event);
private slots:
    void Read_cb(void);
    void Write_cb(qint64 bytes);
    void Read_timer_timeout(void);
    void Write_timer_timeout(void);
};

#endif // SERIALPORT_H
